package com.example.easyexcel.handler;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.fastjson.JSON;
import com.example.easyexcel.annotation.ExcelDropdown;
import com.example.easyexcel.model.UserModel;
import com.example.easyexcel.model.UserModel2;
import com.example.easyexcel.service.ExcelDynamicDropdownDataService;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dangwen1
 * @date 2021年12月03日 11:38
 * @description
 */
public class SpinnerWriterHandler implements SheetWriteHandler {

    private List<Integer> ageList;

    public SpinnerWriterHandler(List<Integer> ageList){
        this.ageList = ageList;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

//        Map<Integer,String[]> mapDropDown = getDropdownMap();
        Map<Integer,String[]> mapDropDown = getDropdownMap2();
        // 获取sheet页
        Sheet sheet = writeSheetHolder.getSheet();
        // 开始设置下拉框
        DataValidationHelper helper = sheet.getDataValidationHelper();
        // 设置下拉框
        for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {
            /*起始行、终止行、起始列、终止列**/
            CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, entry.getKey(), entry.getKey());
            /*设置下拉框数据**/
            DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            // 阻止输入非下拉选项的值
            dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
            dataValidation.setShowErrorBox(true);
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.createErrorBox("提示", "请输入下拉选项中的内容");
            dataValidation.createPromptBox("提示", "请输入下拉选项中的内容");
            sheet.addValidationData(dataValidation);
        }
    }

    /**
      * 依赖传入的ageList，这种方式有一种缺陷，就是当有两个下拉列表的时候，list没法支持，所以有了第二种
      */
    public Map<Integer,String[]> getDropdownMap(){
        Map<Integer,String[]> mapDropDown = new HashMap<>();
        /**
          *  要记得修改实体
          */
        Field[] declaredFields = UserModel.class.getDeclaredFields();
        int i = 0;
        for (Field declaredField : declaredFields) {
            ExcelDropdown annotation = declaredField.getAnnotation(ExcelDropdown.class);
            ExcelProperty property = declaredField.getAnnotation(ExcelProperty.class);
            if (annotation != null) {
                if (property != null) {
                    mapDropDown.put(property.index(), listToArray());
                } else {
                    mapDropDown.put(i, listToArray());
                }
            }
            i++;
        }
        return mapDropDown;
    }

    /**
      * 给每个注解指定一个专有的查询类，根据查询结果进行返回
      */
    public Map<Integer,String[]> getDropdownMap2(){
        Map<Integer,String[]> mapDropDown = new HashMap<>();
        /**
         *  要记得修改实体
         */
        Field[] declaredFields = UserModel2.class.getDeclaredFields();
        int i = 0;
        for (Field declaredField : declaredFields) {
            ExcelDropdown annotation = declaredField.getAnnotation(ExcelDropdown.class);
            ExcelProperty property = declaredField.getAnnotation(ExcelProperty.class);
            if (annotation == null){
                continue;
            }
            String[] source1 = annotation.source();
            if (source1.length > 0) {
                mapDropDown.put(property.index(), source1);
            }
            Class<? extends ExcelDynamicDropdownDataService> clazz = annotation.sourceClass();
            if (clazz != null) {
                try {
                    ExcelDynamicDropdownDataService excelDynamicDropdownDataService = clazz.newInstance();
                    String[] source = excelDynamicDropdownDataService.getSource();
                    mapDropDown.put(property.index(), source);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            i++;
        }
        return mapDropDown;
    }

    public String[] listToArray(){
        String[] ageArray = new String[ageList.size()];
        for (int i = 0; i < ageList.size(); i++) {
            ageArray[i] = ageList.get(i).toString();
        }
        return ageArray;
    }
}
